package com.wei.duowan.bbs.ui.adapter;

import android.graphics.Bitmap;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.utils.GlideApp;

import java.lang.reflect.Field;

/**
 * 作者：赵若位
 * 时间：2019/8/21 15:33
 * 联系方式：1070138445
 * 功能：
 */
public class BaseViewHolder extends RecyclerView.ViewHolder
{
    private SparseArray<View> mSparseArray;
    private AppCompatActivity mActivity;

    private BaseViewHolder(AppCompatActivity activity, @NonNull View itemView)
    {
        super(itemView);
        this.mActivity = activity;
        mSparseArray = new SparseArray<>();
    }


    public static BaseViewHolder getViewHolder(AppCompatActivity activity, @NonNull View itemView)
    {
        return new BaseViewHolder(activity, itemView);
    }


    public <T extends View> T getView(int resourceID)
    {
        View view = mSparseArray.get(resourceID);
        if (view == null)
        {
            view = itemView.findViewById(resourceID);
            if (view != null)
            {
                mSparseArray.put(resourceID, view);
            }
        }
        return (T) view;
    }

    /*TextView Set Txt*/
    public BaseViewHolder setSelected(@IdRes int resourceID, boolean selected)
    {
        if (getView(resourceID) != null)
        {
            getView(resourceID).setSelected(selected);
        }
        return this;
    }




    /*TextView Set Txt*/
    public BaseViewHolder setText(@IdRes int resourceID, @Nullable CharSequence... text)
    {
        if (getView(resourceID) != null && text != null && text.length != 0)
        {
            TextView tv = getView(resourceID);
            StringBuffer buffer = new StringBuffer();
            for (CharSequence c : text)
            {
                buffer.append(c);
            }
            tv.setText(buffer.toString());
        }
        return this;
    }

    /*TextView Set SpannableString*/
    public BaseViewHolder setText(@IdRes int resourceID, @Nullable SpannableString paramString)
    {
        if (getView(resourceID) != null && paramString != null)
        {
            TextView tv = getView(resourceID);
            tv.setText(paramString);
        }
        return this;
    }

    /*TextView set CharSequence*/
    public BaseViewHolder setText(@IdRes int resourceID, @StringRes int stringRes)
    {
        this.setText(resourceID, App.getResource().getString(stringRes));
        return this;
    }

    public BaseViewHolder setTextColor(@IdRes int resourceID, @ColorRes int colorRes)
    {
        View view = getView(resourceID);
        if (view != null && view instanceof TextView)
        {
            ((TextView) view).setTextColor(App.getResource().getColor(colorRes));
        }
        return this;
    }

    /*ImageView set Url*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @Nullable String url)
    {
        if ((!TextUtils.isEmpty(url)) && getView(resourceID) != null)
        {
            GlideApp.with(mActivity)
                    .asBitmap()
                    .placeholder(R.drawable.shape_drawable_error)
                    .error(R.drawable.shape_drawable_error)
                    .centerCrop()
                    .load(url)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }


    /*ImageView set Bitmap*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @DrawableRes int id)
    {
        if (getView(resourceID) != null)
        {
            GlideApp.with(mActivity)
                    .asBitmap()
                    .centerCrop()
                    .load(id)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }


    /*ImageView set Bitmap*/
    public BaseViewHolder setImageResource(@IdRes int resourceID, @Nullable Bitmap bitmap)
    {
        if (getView(resourceID) != null && bitmap != null)
        {
            ImageView image = getView(resourceID);
            image.setImageBitmap(bitmap);
        }
        return this;
    }


    public BaseViewHolder setImageResourceWithField(@IdRes int resourceID, String fileName)
    {
        if (getView(resourceID) != null)
        {
            try
            {
                final Class<?> drawableClass = R.drawable.class;
                final Field field = drawableClass.getField(fileName);
                int drawableId = field.getInt(field.getName());
                ImageView image = getView(resourceID);
                image.setImageResource(drawableId);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        return this;
    }


    /*ImageView set Circle Image*/
    public BaseViewHolder setCircleResource(@IdRes int resourceID, @Nullable String url)
    {
        if ((!TextUtils.isEmpty(url)) && getView(resourceID) != null)
        {
            GlideApp.with(mActivity)
                    .asBitmap()
                    .load(url)
                    .circleCrop()
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }

    /*ImageView set Circle Image*/
    public BaseViewHolder setCircleResource(@IdRes int resourceID, @DrawableRes int id)
    {
        if (getView(resourceID) != null)
        {
            GlideApp.with(mActivity)
                    .asBitmap()
                    .circleCrop()
                    .load(id)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }

    /*User CircleImageView */
    public BaseViewHolder setCircleUserResource(@IdRes int resourceID, @Nullable String url)
    {
        if (getView(resourceID) != null)
        {
            GlideApp.with(mActivity)
                    .asBitmap()
                    .circleCrop()
                    .placeholder(R.mipmap.avatar_placeholder_64)
                    .error(R.mipmap.avatar_placeholder_64)
                    .load(url)
                    .into((ImageView) getView(resourceID));
        }
        return this;
    }


    /*View set Visibility*/
    public BaseViewHolder setVisibility(@IdRes int resourceID, int visibility)
    {
        if (getView(resourceID) != null)
        {
            getView(resourceID).setVisibility(visibility);
        }
        return this;
    }


    /*设置点击事件监听*/
    public BaseViewHolder setOnClickListener(@IdRes int resourceID, @Nullable View.OnClickListener listener)
    {
        if (getView(resourceID) != null && listener != null)
        {
            getView(resourceID).setOnClickListener(listener);
        }
        return this;
    }


}
